﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Data;
using System.Web.UI.HtmlControls;

namespace Phoenix
{
    public partial class Shipping_Schedule : System.Web.UI.Page
    {
        ResourceManager rm;
        CultureInfo ci;
        Phoenix.AppCode.Entities.Port port = new Phoenix.AppCode.Entities.Port();
        Phoenix.AppCode.Entities.Article home = new Phoenix.AppCode.Entities.Article();
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie fenix_lang = Request.Cookies["Culture"];
            if (fenix_lang == null)
            {
                fenix_lang = new HttpCookie("Culture", "en-US");
                fenix_lang.Expires = DateTime.Now.AddHours(24);
                Response.Cookies.Add(fenix_lang);
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                ci = Thread.CurrentThread.CurrentCulture;
                Load_Shipping(ci);
                LoadDB_US();
            }
            else
            {
                if (fenix_lang.Value == "vi-VN")
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Shipping(ci);
                    LoadDB_VN();
                }
                else
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    rm = new ResourceManager("Resources.Language", System.Reflection.Assembly.Load("App_GlobalResources"));
                    ci = Thread.CurrentThread.CurrentCulture;
                    Load_Shipping(ci);
                    LoadDB_US();
                }
            }
            if (!Page.IsPostBack)
            {
                Load_Port();
            }
        }
        public void Load_Shipping(CultureInfo ci)
        {
            string ship_title = rm.GetString("ship_title", ci);
            string ship_port = rm.GetString("ship_port", ci);
            string ship_from = rm.GetString("ship_from", ci);
            string ship_to = rm.GetString("ship_to", ci);
            lb_title.Text = ship_title;
            lb_port.Text = ship_port;
            lb_from.Text = ship_from;
            lb_to.Text = ship_to;
        }
        private void LoadDB_VN()
        {
            int id = 1;
            string lang = "vi-VN";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
        private void LoadDB_US()
        {
            int id = 1;
            string lang = "en-US";
            home.ID_ArticleCategory = id;
            home.Language = lang;
            DataTable dt = Phoenix.AppCode.Bussiness.ArticleService.ArticleLoadService(home);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                string dtitle = dr["TitleName"].ToString();
                string htitle = dr["TitleHead"].ToString();
                string des = dr["DescriptionHead"].ToString();
                string key = dr["KeywordHead"].ToString();
                HtmlHead headTag = (HtmlHead)this.Header;
                if (htitle.Length < 2)
                {
                    headTag.Title = dtitle;
                }
                else
                {
                    headTag.Title = htitle;
                }
                HtmlMeta PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "description";
                PagemetaTag.Content = des;
                headTag.Controls.Add(PagemetaTag);

                PagemetaTag = new HtmlMeta();
                PagemetaTag.Name = "keywords";
                PagemetaTag.Content = key;
                headTag.Controls.Add(PagemetaTag);
            }
        }
        private void Load_Port()
        {
            DataTable dt = Phoenix.AppCode.Bussiness.PortService.PortList(port);
            if (dt.Rows.Count > 0)
            {
                
                DataRow dr = dt.Rows[0];
                drop_port.DataSource = dt;
                drop_port.DataTextField = "PortName";
                drop_port.DataValueField = "ID_Port";
                drop_port.DataBind();
                drop_port.Items.Insert(0, new ListItem("-------------ALL-------------", "0"));
            }
        }

        protected void bt_sumit_Click(object sender, EventArgs e)
        {
            string str = "";
            int id = int.Parse(drop_port.SelectedValue.ToString());
            string from = hidden_from.Value;
            string to = hidden_to.Value;
            //DateTime from = new DateTime();
            //DateTime to = new DateTime();
            //from.ToShortDateString();
            //to.ToShortDateString();
            //from = Convert.ToDateTime(hidden_from.Value.Trim());
            //to = Convert.ToDateTime(hidden_to.Value.Trim());
            //string from = "09/27/2012";
            //string to = "10/31/2012";
            DataTable dt = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingGetDate(id,from,to);
            
            str += "<table class='sr_main'><tbody>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                    DataRow dr = dt.Rows[i];
                    int idport = int.Parse(dr["ID_Port"].ToString());
                    DataTable dtport = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingGetListID(idport);
                    DataRow drport1 = dtport.Rows[0];
                    DataTable dtship = Phoenix.AppCode.Bussiness.PortShippingScheduleService.PortShippingGetDate(idport, from, to);

                    if (i != 0)
                    {
                        if (idport != int.Parse(dt.Rows[i - 1]["ID_Port"].ToString()))
                        {
                            str += "<tr class='sr_title'><td>" + drport1["PortName"].ToString() + "</td></tr>";
                            str += "<tr><td><table class='c_r_ship'><thead><tr class='c_r_title'><th class='col1'>STT</th><th class='col2'>Port</th><th class='col3'>FEEDER</th><th class='col4'>VOY</th><th class='col5'>ETD HCM</th><th class='col6'>CONNECTING VESSEL</th><th class='col7'>ETD</th><th class='col8'>ETA DEST</th></tr></thead><tbody>";
                            DataRow drship = dtship.Rows[0];
                            int idshop = int.Parse(drship["ID_Port"].ToString());
                            //if (idshop != idport)
                            //{
                            int z = 1;
                            for (int k = 0; k < dtship.Rows.Count; k++)
                            {
                                str += "<tr class='c_r_main'>";
                                str += "<td>" + z + "</td>";
                                str += "<td>" + dtship.Rows[k]["PortName"] + "</td>";
                                str += "<td>" + dtship.Rows[k]["FEEDER"] + "</td>";
                                str += "<td>" + dtship.Rows[k]["VOY"] + "</td>";
                                str += "<td>" + String.Format("{0:d}", dtship.Rows[k]["ETDHCM"]) + "</td>";
                                str += "<td>" + dtship.Rows[k]["CONNECTINGVESSEL"] + "</td>";
                                str += "<td>" + dtship.Rows[k]["ETD"] + "</td>";
                                str += "<td>" + String.Format("{0:d}", dtship.Rows[k]["ETADEST"]) + "</td>";
                                str += "</tr>";
                                z++;
                            }
                            str += "</tbody></table></td></tr>";
                        }
                    }
                    else
                    {
                        str += "<tr class='sr_title'><td>" + drport1["PortName"].ToString() + "</td></tr>";
                        str += "<tr><td><table class='c_r_ship'><thead><tr class='c_r_title'><th class='col1'>STT</th><th class='col2'>Port</th><th class='col3'>FEEDER</th><th class='col4'>VOY</th><th class='col5'>ETD HCM</th><th class='col6'>CONNECTING VESSEL</th><th class='col7'>ETD</th><th class='col8'>ETA DEST</th></tr></thead><tbody>";
                        DataRow drship = dtship.Rows[0];
                        int idshop = int.Parse(drship["ID_Port"].ToString());
                        //if (idshop != idport)
                        //{
                        int z = 1;
                        for (int k = 0; k < dtship.Rows.Count; k++)
                        {
                            str += "<tr class='c_r_main'>";
                            str += "<td>" + z + "</td>";
                            str += "<td>" + dtship.Rows[k]["PortName"] + "</td>";
                            str += "<td>" + dtship.Rows[k]["FEEDER"] + "</td>";
                            str += "<td>" + dtship.Rows[k]["VOY"] + "</td>";
                            str += "<td>" + String.Format("{0:d}", dtship.Rows[k]["ETDHCM"]) + "</td>";
                            str += "<td>" + dtship.Rows[k]["CONNECTINGVESSEL"] + "</td>";
                            str += "<td>" + dtship.Rows[k]["ETD"] + "</td>";
                            str += "<td>" + String.Format("{0:d}", dtship.Rows[k]["ETADEST"]) + "</td>";
                            str += "</tr>";
                            z++;
                        }

                        str += "</tbody></table></td></tr>";
                    }
                
            }
            str += "</tbody></table>";
            text_content.Text = str;
        }
    }
}